package  
{
	import flash.display.InterpolationMethod;
	import org.flixel.FlxGroup;
	import org.flixel.FlxSprite;
	import org.flixel.FlxState;
	import org.flixel.FlxG;
	import org.flixel.FlxU;
	import org.flixel.*;
	import org.flixel.system.input.Keyboard;
	import player.PlayerController;
	import player.Salazar;
	import pessoas.*;
	
	
	/**
	 * ...
	 * @author Lucas Mattos
	 */
	public class ShopState extends FlxState 
	{
		[Embed(source = "../assets/som/main_music_Drink-Up-Westley.mp3")]
		public var mainMusic:Class;
		
		[Embed(source = "../assets/telas/shop.png")]
		public var background:Class;
		public var backgroundSprite:FlxSprite;
		
		public var keyboard:Keyboard = new Keyboard();
		
		// Items
		
		public static var ItemImposto1:Item = new Item("Raise Taxes 1", "Get more money from citizens", 300);
		public static var ItemImposto2:Item = new Item("Raise Taxes 2", "Get even MORE money from citizens", 6000);
		public static var ItemImposto3:Item = new Item("Raise Taxes 3", "Why? Why not?", 19000);
		
		public static var ItemPropina1:Item = new Item("Bribe 1", "Stay less time in jail", 600);
		public static var ItemPropina2:Item = new Item("Bribe 2", "Stay even less time in jail", 5000);
		public static var ItemPropina3:Item = new Item("Bribe 3", "You're way above jail, you know?", 20000);
		
		public static var ItemCensura1:Item = new Item("Censorship 1", "Reporters don't keep much pace with you.", 700);
		public static var ItemCensura2:Item = new Item("Censorship 2", "Reporters ask a few questions and go away.", 4000);
		public static var ItemCensura3:Item = new Item("Censorship 3", "Pesky, hypocrite snippersnappers!", 17000);
		
		// equips
		public static var equipSaidaPrisao:Item = new Item("Jail Breaker", "Get out of jail immediately (once per round)", 5000);
		public static var equipAcessorImprensa:Item = new Item("Press Advisor", "Blocks reporters questions (once per round)", 6000);
		
		private var firstLawShown:uint = 0;
		private var numLawsShown:uint = 3;
		private var lawsToShowText:Array;
		private var laws:Array;
		private var lawButtons:Array;
		private var changedLaws:Boolean = false;
		
		private var lawsY:uint = 155;
		private var lawsX:uint = 60;
		private var lawsHeight:uint = 125;
		private var lawsWidth:uint = 330;
		private var lawButtonOffsetY:uint = lawsHeight-50;
		private var lawButtonOffsetX:uint = lawsWidth - 100;
		
		private var setaUpButton:FlxButton;
		private var setaDownButton:FlxButton;
		private var nextLevelButton:FlxButton;
		private var eq1Button:FlxButton;
		private var eq2Button:FlxButton;
		
		private var eq1Text:FlxText;
		private var eq2Text:FlxText;
		
		private var buyButton1:FlxButton;
		private var buyButton2:FlxButton;
		private var buyButton3:FlxButton;
		
		private var item1:Item;
		private var item2:Item;
		private var item3:Item;
		
		private var moneyText:FlxText;
		
		public function ShopState() 
		{
			super();
			
			backgroundSprite = new FlxSprite(0, 0, background);
			add(backgroundSprite);
			
			laws = new Array();
		}
		
		override public function create():void
		{
			FlxG.playMusic(mainMusic, 1);
			
			if (PlayerData.level == 0)
			{
				laws.push(ItemImposto1,  ItemPropina1, ItemCensura1);
			}
			
			if (PlayerData.level == 1)
			{
				laws.push(ItemImposto1,  ItemPropina1, ItemCensura1, ItemImposto2,  ItemPropina2, ItemCensura2);
			}
			
			if (PlayerData.level >= 2)
			{
				laws.push(ItemImposto1,  ItemPropina1, ItemCensura1, ItemImposto2,  ItemPropina2, ItemCensura2, ItemImposto3,  ItemPropina3, ItemCensura3);
			}
			
			mainButtons();
			initTexts()
			updateLawsShown();
		}
		
		override public function update():void
		{		
			super.update();
			
			moneyText.text = "Money: \n$" + PlayerData.dinheiro_atual;
		}
		
		override public function draw():void
		{		
			super.draw();
			
			for each (var currentLaw:FlxText in lawsToShowText)
			{								
				currentLaw.draw();
			}
			
		}
		
		private function mainButtons():void
		{
			var button:FlxButton;
					
			
			//nxt Level
			nextLevelButton = new FlxButton(475, 520, PlayerData.levelNames[PlayerData.level+1] + "\n$" + PlayerData.levelPrices[PlayerData.level+1]);
			nextLevelButton.changeButtonSize(2, 2.5);
			
			if (PlayerData.dinheiro_atual < PlayerData.levelPrices[PlayerData.level + 1])
			{
				nextLevelButton.onDown = null;
				nextLevelButton.onUp = null;
				nextLevelButton.color = 0xAAAAAA;
			}
			else
			{
				nextLevelButton.onUp = nextLevel;
				nextLevelButton.color = 0xd5ce91;
			}
			if (PlayerData.level < 4)
				add(nextLevelButton);
			
			//Reelection
			button = new FlxButton(475 + 170, 520, "Run mandate", startSprint);
			button.changeButtonSize(2, 2.5);
			button.color = 0xd5ce91;
			add(button);
			
			//Subir e Descer
			setaUpButton = new FlxButton(lawsX+125, 127, "/\\", lawsUp);
			add(setaUpButton);
			
			setaDownButton = new FlxButton(lawsX+125, 527, "\\/", lawsDown);
			add(setaDownButton);
						
			if (firstLawShown == 0)
				setaUpButton.color = 0xAAAAAA;
			else
				setaUpButton.color = 0xd5ce91
				
			if (firstLawShown == laws.length-3)
				setaDownButton.color = 0xAAAAAA;
			else
				setaDownButton.color = 0xd5ce91;
				
			//buy law
			buyButton1 = new FlxButton(lawsX + lawButtonOffsetX, lawsY + lawButtonOffsetY, "Bought", buy1);
			buyButton2 = new FlxButton(lawsX + lawButtonOffsetX, lawsY+lawsHeight + lawButtonOffsetY, "Bought", buy2);
			buyButton3 = new FlxButton(lawsX + lawButtonOffsetX, lawsY+lawsHeight*2 + lawButtonOffsetY, "Bought", buy3);	
			
			add(buyButton1);
			add(buyButton2);
			add(buyButton3);
			
			//buy equips
			eq1Button = new FlxButton(650, 260, "Buy", buyeq1);
			eq2Button = new FlxButton(650, 430, "Buy", buyeq2);
			
			add(eq1Button);
			add(eq2Button);
			
			updateButtons();
		}
		
		private function initTexts():void
		{
			var text:FlxText;
			var nameAndPrice:FlxText;			
			var explanation:FlxText;
			
			moneyText = new FlxText(20, 15, 150, "Money: \n$" + PlayerData.dinheiro_atual);
			moneyText.setFormat( null, 18, 0xdfddcd, null, 1);
			moneyText.x = Main.windowWidth - moneyText.width - 20;
			add(moneyText);
					
			text = new FlxText(20, 15, 400, PlayerData.levelNames[PlayerData.level]+ "'s Shop");
			text.setFormat( null, 30, 0xdfddcd, null, 1);			
			add(text);
			
			text = new FlxText(65, 90, 400, "Buy a Law:");
			text.setFormat( null, 20, 0xdfddcd, null, 1);		
			add(text);
			
			text = new FlxText(463, 90, 400, "Choose a Benefit:");
			text.setFormat( null, 20, 0xdfddcd, null, 1);	
			add(text);
			
			nameAndPrice = new FlxText(460, 140, 300);
			nameAndPrice.setFormat( null, 20, 0xdfddcd, null, 1);
				
			explanation = new FlxText(460, 140+30, 300);
			explanation.setFormat( null, 15, 0xdfddcd, null, 1);
				
			nameAndPrice.text = "$" + equipSaidaPrisao.preco + " " + equipSaidaPrisao.nome +"\n";
			explanation.text = equipSaidaPrisao.explicacao;
			
			add(nameAndPrice);
			add(explanation);
			
			nameAndPrice = new FlxText(460, 300, 300);
			nameAndPrice.setFormat( null, 20, 0xdfddcd, null, 1);
				
			explanation = new FlxText(460, 300+30, 300);
			explanation.setFormat( null, 15, 0xdfddcd, null, 1);
				
			nameAndPrice.text = "$" + equipAcessorImprensa.preco + " " + equipAcessorImprensa.nome +"\n";
			explanation.text = equipAcessorImprensa.explicacao;
			
			add(nameAndPrice);
			add(explanation);
			
			
		}
		
		private function updateLawsShown():void
		{
			var i:uint = 0;
			var j:uint = 0;
			lawsToShowText = new Array();
			lawButtons = new Array();
					
			var currX:uint = lawsX;
			var currY:uint = lawsY;
			
			var nameAndPrice:FlxText;			
			var explanation:FlxText;
			var buttonText:String;
			var buttonColor:uint;
			
			var currentLaw:Item;
			
			for (i = firstLawShown; (i < firstLawShown + numLawsShown) && (i < laws.length) ; i++)
			{
				currentLaw = laws[i] as Item;
				
				nameAndPrice = new FlxText(currX, currY, lawsWidth);
				nameAndPrice.setFormat( null, 20, 0xdfddcd, null, 1);
				
				explanation = new FlxText(currX, currY+30, lawsWidth);
				explanation.setFormat( null, 15, 0xdfddcd, null, 1);
				
				nameAndPrice.text = "$" + currentLaw.preco + " " + currentLaw.nome +"\n";
				explanation.text = currentLaw.explicacao;
											
				if (currentLaw.comprado)
				{
					buttonText = "Bought";
					buttonColor = 0x9dbc95;
				}
				else if (currentLaw.preco > PlayerData.dinheiro_atual)
				{
					buttonText = "No Money";
					buttonColor = 0xbca395;
				}
				else
				{
					buttonText = "Buy";
					buttonColor = 0xd5ce91;
				}
				
				lawsToShowText.push(nameAndPrice);
				lawsToShowText.push(explanation);
				
				if (j == 0)
				{
					buyButton1.color = buttonColor;
					buyButton1.label.text = buttonText;
					item1 = currentLaw;
				}
				if (j == 1)
				{
					buyButton2.color = buttonColor;
					buyButton2.label.text = buttonText;
					item2 = currentLaw;

				}
				if (j == 2)
				{
					buyButton3.color = buttonColor;
					buyButton3.label.text = buttonText;
					item3 = currentLaw;

				}
				
				currY += lawsHeight;
				j++;
			}
		}
		
		private function updateButtons():void
		{
			if (firstLawShown == 0)
				setaUpButton.color = 0xAAAAAA;
			else
				setaUpButton.color = 0xd5ce91
				
			if (firstLawShown == laws.length-3)
				setaDownButton.color = 0xAAAAAA;
			else
				setaDownButton.color = 0xd5ce91
				
			if (PlayerData.dinheiro_atual < PlayerData.levelPrices[PlayerData.level + 1])
			{
				nextLevelButton.onDown = null;
				nextLevelButton.onUp = null;
				nextLevelButton.color = 0xAAAAAA;
			}
			else
			{
				nextLevelButton.onUp = nextLevel;
				nextLevelButton.color = 0xd5ce91;
			}
			
			if (PlayerData.hasSaidaPrisao)
			{
				if(PlayerData.equipedSaidaPrisao)
				{
					eq1Button.color = 0xAAAAAA;
					eq1Button.label.text = "Activated";
				}
				else
				{
					eq1Button.color = 0x9dbc95;
					eq1Button.label.text = "Activate";
				}					
			}
			else
			{
				if(PlayerData.dinheiro_atual >= equipSaidaPrisao.preco)
				{
					eq1Button.color = 0xd5ce91;
					eq1Button.label.text = "Buy";
				}
				else
				{
					eq1Button.color = 0xbca395;
					eq1Button.label.text = "No Money";
				}					
			}
			
			if (PlayerData.hasAcessorImprensa)
			{
				if(PlayerData.equipedAcessorImprensa)
				{
					eq2Button.color = 0xAAAAAA;
					eq2Button.label.text = "Activated";
				}
				else
				{
					eq2Button.color = 0x9dbc95;
					eq2Button.label.text = "Activate";
				}					
			}
			else
			{
				if(PlayerData.dinheiro_atual >= equipAcessorImprensa.preco)
				{
					eq2Button.color = 0xd5ce91;
					eq2Button.label.text = "Buy";
				}
				else
				{
					eq2Button.color = 0xbca395;
					eq2Button.label.text = "No Money";
				}					
			}
		}
		
		private function lawsUp():void
		{
			if (firstLawShown == 0) return;
			firstLawShown--;
			updateLawsShown();
			updateButtons();
		}
		
		private function lawsDown():void
		{
			if (firstLawShown == laws.length-3) return;
			firstLawShown++;
			updateLawsShown();
			updateButtons();
		}
		
		private function buy1():void
		{
			item1.Comprar();
			updateLawsShown();
			updateButtons();
		}
		
		private function buy2():void
		{
			item2.Comprar();
			updateLawsShown();
			updateButtons();
		}
		
		private function buy3():void
		{
			item3.Comprar();
			updateLawsShown();
			updateButtons();
		}
		
		private function buyeq1():void
		{
			if (PlayerData.equipedSaidaPrisao) return;
			
			if (PlayerData.hasSaidaPrisao)
				PlayerData.EquiparSaidaPrisao();
			else
			{
				if (PlayerData.dinheiro_atual >= equipSaidaPrisao.preco)
					equipSaidaPrisao.Comprar();
				else
					return;
			}
			updateLawsShown();
			updateButtons();
		}
		
		private function buyeq2():void
		{
			if (PlayerData.equipedAcessorImprensa) return;
			
			if (PlayerData.hasAcessorImprensa)
				PlayerData.EquiparAcessorImprensa();
			else
			{
				if (PlayerData.dinheiro_atual >= equipAcessorImprensa.preco)
					equipAcessorImprensa.Comprar();
				else
					return;
			}
			updateLawsShown();
			updateButtons();
		}
		
		private function nextLevel():void
		{
			PlayerData.level++;
			PlayerData.dinheiro_atual -= PlayerData.levelPrices[PlayerData.level];
			if (PlayerData.level == 4)
				FlxG.switchState(new LastScreen);
			else
				FlxG.switchState(new ShopState);
		}
		
		private function startSprint():void
		{
			FlxG.switchState(new MainState());
		}
	}
}